<?php

declare(strict_types=1);

namespace app\admin\filter\auth;

use sheep\filter\BaseFilter;
use think\facade\Request;
use think\db\Query;
use sheep\model\auth\Admin;

/**
 * 管理员日志
 */
class AdminLogFilter extends BaseFilter
{
    protected $keywordFields = [];


    /**
     * 管理员相关信息 whereExists 查询
     *
     * @param Query $query
     * @param string|array $admin   查询数据
     * @return Query
     */
    public function admin($query, $admin)
    {
        // 当前表名
        $current_name = $query->getTable();
        return $query->whereExists(function ($query) use ($current_name, $admin) {
            // 子查询表名
            $table_name = (new Admin())->db()->getTable();
            // 子查询条件
            $query->table($table_name)->where($current_name . '.admin_id=' . $table_name . '.id');
            // 拼接查询条件
            foreach ($admin as $field => $value) {
                $query = $this->builderFilter($query, $field, $value);
            }

            return $query;
        });
    }
}
